package com.abocode.aichat.data;
import com.abocode.aichat.exception.EntityFormatException;
import com.abocode.aichat.util.UUIDUtils;

/**
 * @author guanxf
 */
public class EntityImpl implements Entity {

    public static final String ENTITY_FORMAT_ERROR_MESSAGE = "entity must be of format node@domain/resource";
    private String node;
    private String domain;

    private String resource;

    private boolean resourceSet;

    private String presence="online";

    @Override
    public String getPresence() {
        return presence;
    }

    @Override
    public void setPresence(String presence) {
        this.presence = presence;
    }

    public EntityImpl(String node, String domain, String resource, boolean resourceSet) {
        this.node = node;
        this.domain = domain;
        this.resource = resource;
        this.resourceSet = resourceSet;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()){
            return false;
        }
        EntityImpl entity = (EntityImpl) o;
        return node.equals(entity.node);
    }

    @Override
    public int hashCode() {
        return node.hashCode();
    }

    @Override
    public String getNode() {
        return node;
    }

    /***
     * //node@domain/resource
     * @return
     */
    @Override
    public String getName() {
        return node + CHAR_AT + domain+CHAR_SLASH+resource;
    }

    @Override
    public String getResource() {
        return resource;
    }

    @Override
    public String getFullName() {
        if (!isResourceSet()) {
            return getName();
        }
        return node + CHAR_AT + domain +CHAR_SLASH+resource;
    }

    public void setNode(String node) {
        this.node = node;
    }

    @Override
    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public void setResource(String resource) {
        this.resource = resource;
    }

    public void setResourceSet(boolean resourceSet) {
        this.resourceSet = resourceSet;
    }

    @Override
    public boolean isResourceSet() {
        return resourceSet;
    }

    public static final String CHAR_AT = "@";

    public static final String CHAR_SLASH = "/";

    public static EntityImpl parse(String entity)  {
        String node = null;
        String domain = null;
        String resource = null;
        boolean resourceSet = false;
        if (entity == null) {
            throw new EntityFormatException("entity must not be NULL");
        }
        if (entity.contains(CHAR_AT)) {
            String[] parts = entity.split(CHAR_AT);
            if (parts.length != 2) {
                throw new EntityFormatException(ENTITY_FORMAT_ERROR_MESSAGE);
            }
            node = parts[0];
            entity = parts[1];
            domain = parts[1];
        }
        if (entity.contains(CHAR_SLASH)) {
            String[] parts = entity.split(CHAR_SLASH);
            if (parts.length != 2) {
                throw new EntityFormatException(ENTITY_FORMAT_ERROR_MESSAGE);
            }
            domain = parts[0];
            resource = parts[1];
            resourceSet = true;
        }
        return new EntityImpl(node, domain, resource, resourceSet);
    }
}
